package com.itheima.stock.controller;

import com.itheima.stock.common.ResponseCode;
import com.itheima.stock.ex.CustomerException;
import com.itheima.stock.service.SysUserService;
import com.itheima.stock.vo.req.LoginReqVo;
import com.itheima.stock.vo.req.UserPageReqVo;
import com.itheima.stock.vo.resp.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author Zack
 * @Date 2022/12/5 19:36
 * @Version 1.0
 */
@RestController
@RequestMapping("/api")
@Slf4j
public class UserController {
    @Autowired
    SysUserService sysUserService;
    /**
     * 用户登录功能实现
     * @param vo
     * @return
     */
    @PostMapping("/login")
    public R<LoginRespVo> login(@RequestBody LoginReqVo vo) throws CustomerException {
        if (Objects.isNull(vo)
                || StringUtils.isBlank(vo.getCode())
                || StringUtils.isBlank(vo.getUsername())
                || StringUtils.isBlank(vo.getPassword())
                || StringUtils.isBlank(vo.getRkey())) {
            throw new CustomerException(ResponseCode.DATA_ERROR);
        }
        R<LoginRespVo> r= sysUserService.login(vo);
        return r;
    }

    /**
     * 生成验证码
     * @return
     */
    @GetMapping("/captcha")
    public R<CaptchaRespVo> generateCaptcha(){
        return sysUserService.generateCaptcha();
    }

    @PostMapping("/users")
    public R<PageResult<UserPageRespVo>> findUserPage(@RequestBody UserPageReqVo reqVo) throws CustomerException {
        return sysUserService.findPageVo(reqVo);
    }

}
